<?php

namespace app\admin\controller\wdgroup;

use app\common\controller\Backend;
use app\common\model\Wdcate as WdcateModel;
use fast\Tree;
use think\Db;
/**
 * 闻道分类管理
 *
 * @icon fa fa-list
 * @remark 用于管理闻道分类，支持无限级分类
 */
class Wdcate extends Backend
{
    protected $model = null;
    protected $categorylist = [];
    protected $noNeedRight = ['selectpage'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new WdcateModel();
        $this->categorylist = $this->model->where('pid',0)->where('status','normal')->column('id,name');
        $this->view->assign("parentList", array_merge(['0'=>'顶级分类'],$this->categorylist));
        $this->view->assign("userLevelList", array_merge([0 => '请选择'],model('userGroup')->where('status','normal')->column('id,name')));
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $v->image = $v->image ? cdnurl($v->image, true) : letter_avatar($v->name);
                if(empty($v->pid )){
                    $v->prantname = '顶级分类';
                }else{
                    $v->prantname = Db::name('Wdcate')->where('id',$v->pid)->value('name');
                }
                
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    
    /**
     * Selectpage搜索
     *
     * @internal
     */
    public function selectpage()
    {
        return parent::selectpage();
    }

}